package com.iss.umservice.common.xml;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;


/**
 * <p>
 * [���ܼ���] ʹ��DOM����xml�ļ�
 * <p>
 * <p>
 * [��ϸ����] ʹ��DOM����xml�ļ�
 * </p>
 * @author  gcxu
 * @version  [GZT 1.00, Aug 12, 2010]
 * @see  [�����/����]
 * @since  [GZT]
 */
public class XmlDomParser
{
    /**
     * Ĭ�Ϲ��캯��
     */
    private XmlDomParser()
    {
    }
    
    /**  
     * calsspath��ȡ�����ļ� 
     * @param xmlPath �ļ�·��
     * @return XmlDomNode
     * @throws ServiceException 
     */
    public static XmlDomNode parser(String xmlPath)
    {
        // calsspath��ȡ�����ļ�
        InputStream is = XmlDomParser.class.getResourceAsStream(xmlPath);
        return parser(is);
    }
    
    /**
     * 
     * <p>
     * [���ܼ���]��InputSource��ʽ��ȡxml�ļ�
     * <p>
     * <p>
     * [��ϸ����]��InputSource��ʽ��ȡxml�ļ�
     * </p>
     * @param is InputSource
     * @return XmlDomNode
     * @exception throws [Υ������] [Υ��˵��]
     * @see [�ࡢ��#��������#��Ա]
     */
    public static XmlDomNode parser(InputSource is)
    {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = null;
        try
        {
            db = dbf.newDocumentBuilder();
        }
        catch (ParserConfigurationException e)
        {
            return new XmlDomNode();
        }
        
        try
        {
            return new XmlDomNode(db.parse(is));
        }
        catch (SAXException e1)
        {
            return new XmlDomNode();
        }
        catch (IOException el)
        {
            return new XmlDomNode();
        }
    }
    
    /**
     * 
     * <p>
     * [���ܼ���] ����ļ�·������ȥ��ȡxml
     * <p>
     * <p>
     * [��ϸ����] ����ļ�·������ȥ��ȡxml
     * </p>
     * @param file �ļ�����
     * @return XmlDomNode
     * @exception throws [Υ������] [Υ��˵��]
     * @see [�ࡢ��#��������#��Ա]
     */
    public static XmlDomNode parser(File file)
    {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = null;
        try
        {
            db = dbf.newDocumentBuilder();
        }
        catch (ParserConfigurationException e)
        {
            return new XmlDomNode();
        }
        
        try
        {
            return new XmlDomNode(db.parse(file));
        }
        catch (SAXException e1)
        {
            return new XmlDomNode();
        }
        catch (IOException el)
        {
            return new XmlDomNode();
        }
    }
    
    /**
     * 
     * <p>
     * [���ܼ���] ������ȡxml�ļ�
     * <p>
     * <p>
     * [��ϸ����] ������ȡxml�ļ�
     * </p>
     * @param is �ļ���
     * @return XmlDomNode
     * @exception throws [Υ������] [Υ��˵��]
     * @see [�ࡢ��#��������#��Ա]
     */
    public static XmlDomNode parser(InputStream is)
    {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = null;
        try
        {
            db = dbf.newDocumentBuilder();
        }
        catch (ParserConfigurationException e)
        {
            return new XmlDomNode();
        }
        
        try
        {
            return new XmlDomNode(db.parse(is));
        }
        catch (SAXException e1)
        {
            return new XmlDomNode();
        }
        catch (IOException el)
        {
            return new XmlDomNode();
        }
    }
}